﻿@model Sealight.Core.DataAdapter.product_variant
@{
    ViewBag.Title = "Cập nhật sản phẩm";
    Layout = "~/Areas/Admin/Views/Shared/__AdminLayout.cshtml";
}
@section _Head{
<style type="text/css">

    .field-validation-error
    {
        position: absolute;
        display: block;
    }
    
    * html .field-validation-error { position: relative; }
    *+html .field-validation-error { position: relative; }
    
    .field-validation-error span
    {
        position: absolute;
        white-space: nowrap;
        color: red;
        padding: 17px 5px 3px;
        background: transparent url('@Url.Content("~/Content/Common/validation-error-message.png") ') no-repeat 0 0;
    }
    
    /* in-form editing */
    .t-edit-form-container
    {
        width: 350px;
        margin: 1em;
    }
    
    .t-edit-form-container .editor-label,
    .t-edit-form-container .editor-field
    {
        padding-bottom: 1em;
        float: left;
    }
    
    .t-edit-form-container .editor-label
    {
        width: 30%;
        text-align: right;
        padding-right: 3%;
        clear: left;
    }
    
    .t-edit-form-container .editor-field
    {
        width: 60%;
    }
</style>

   <script type="text/javascript">
       $(function () {
           $("#product_tabs").tabs();
       });
       $(document).ready(function () {
           //<![CDATA[
           // Replace the <textarea id="editor1"> with an CKEditor instance.
           var description_editor;
           if (description_editor)
               description_editor.destroy();
           description_editor = CKEDITOR.replace('description_field', {
               filebrowserImageBrowseUrl: '/file/Index',
               filebrowserImageUploadUrl: '/file/UploadImageWithCKEditor?folder=editors',
               height: 500,
               filebrowserWindowWidth: '640',
               filebrowserWindowHeight: '480'

           });
           // The instanceReady event is fired when an instance of CKEditor has finished
           // its initialization.
           CKEDITOR.on('instanceReady', function (ev) {
               // Show the editor name and description in the browser status bar.

           });
           //]]>
           $('#available_start_date_field').datepicker({
               defaultDate: new Date(),
               dateFormat: 'dd/mm/yy'
           });
           $('#available_end_date_field').datepicker({
               defaultDate: new Date(),
               dateFormat: 'dd/mm/yy'
           });

           var upload = new AjaxUpload('#file_field', {
               action: '/file/UploadFile',
               data: { 'folder': 'pictures' },
               responseType: 'true',
               onComplete: function (file, response) {
                   if (response != '0') {
                       $("#hidden_image_name").val(response);
                       var img_src = "/Content/images/pictures/" + response;
                       var img_url = GetImageUrl(img_src, 100, 111, false, 100);
                       $("#picture_field_image").attr("src", img_url);
                   }
                   else {

                       ShowErrorMessage('invalid file extension (jpg|png|jpeg|gif).');
                   }
               }
           });

           var container = $('div.container_error');
           $("#data_form").validate({
               rules: {
                   product_name_field: {// compound rule 
                       required: true
                   }
               },
               messages: {
                   product_name_field: {
                       required: "Vui lòng nhập tên sản phẩm."
                   }
               },
               errorElement: "li",
               errorContainer: container,
               errorLabelContainer: $(".error", container)
            ,
               submitHandler: function (form) {
                   var oEditorDetail = CKEDITOR.instances.description_field;
                   var detail = oEditorDetail.getData();
                   var disabledBuy = false;

                   if ($("#disable_buy_button_field").attr('checked') == 'checked' || $("#disable_buy_button_field").attr('checked') == 'true') {
                       disabledBuy = true;
                   }

                   var data = {
                       id: $("#product_variant_id").val(),
                       product_id: $("#product_id").val(),
                       name: $("#product_name_field").val(),
                       SKU: $("#product_sku_field").val(),
                       description: detail,
                       disable_buy_button: disabledBuy,
                       price: $("#price_field").val(),
                       old_price: $("#old_price_field").val(),
                       product_cost: $("#product_cost_field").val(),
                       weight: $("#weight_field").val(),
                       length: $("#length_field").val(),
                       width: $("#width_field").val(),
                       height: $("#height_field").val(),
                       input_available_start_date: $("#available_start_date_field").val(),
                       input_available_end_date: $("#available_end_date_field").val(),
                       order_minimum_quantity: $('#minimum_cart_quantity_field').val(),
                       order_maximum_quantity: $('#maximum_cart_quantity_field').val(),
                       picture:
                       {
                           id: $("#hidden_image_id").val(),
                           picture_url: $("#hidden_image_name").val()
                       }

                   };

                   JsUtils.ShowLoader('#data_form', 'Lưu...');
                   $.ajax({
                       type: "POST",
                       url: "/admin/product/_SaveProductVariantData",
                       data: JSON.stringify(data),
                       traditional: true,
                       dataType: "json",
                       contentType: 'application/json',
                       error: function (xhr, status, error) { },
                       success: function (d) {
                           if (d != null) {
                               if (d.OperationComplete != null && d.OperationComplete == false && d.ErrorCode != null) {
                                   if (d.ErrorFriendlyMessage != null) {
                                       ShowErrorMessage(d.ErrorFriendlyMessage);
                                   }
                                   return;
                               }
                               else {
                                   ShowErrorMessage("Lưu thành công", "thông báo");
                                   //$('#data_form').get(0).reset();
                                   window.location = "/admin/Product/AddUpdateProductVariant/" + d.id;
                               }
                           }
                       },
                       complete: function (d) {
                           JsUtils.hideLoader('#data_form');
                       },
                       cache: false
                   }); //end .ajax call
               }
           }); //end form.validate   

       });


    </script>

}
@if (Model != null)
{
<h2>
    Sửa sản phẩm biến thể</h2>
}
else
{
<h2>
    Thêm mới sản phẩm biến thể</h2>
}
<div>
    <form id="data_form" method="post" action="" enctype="multipart/form-data">
    <div class="options">
        <input type="submit" value="Lưu" id="btnAddNew" class="adminButtonBlue" title="Lưu" />
    </div>
    <div class="container_error">
        <h5>
            Vui lòng kiểm tra lại các lỗi...</h5>
        <ol class="error">
        </ol>
    </div>

    <div id="product_tabs">
        <ul>
            <li><a href="#tabs-1">Thông tin sản phẩm biến thể</a></li>
            
            @if (Model != null)
            {
             <li><a href="#tabs-2">Thuộc tính Sản phẩm</a></li>
            }
        </ul>
        <div id="tabs-1">
            @Html.Partial("_AddUpdateProductVariantInfo", Model)
        </div>      
        @if (Model != null)
        {
         <div id="tabs-2">
            @Html.Action("ListAttributeForProductVariant", "Product", new { productVariantId = Model.id })
        </div>
        }

    </div>
    </form>
</div>
